package com.gdth.bank.ui;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gdth.bank.BankApplication;
import com.gdth.bank.R;
import com.gdth.bank.activity.MainTabActivity;
import com.gdth.bank.activity.UserActivity;
import com.gdth.bank.adapter.GncdListAdapter;
import com.gdth.bank.utils.MenuUtil;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseFrameLayout;
import com.gdth.database.bean.TAppYh;
import com.gdth.model.ImageItem;
import com.gdth.utils.DesensitizeUtil;

public class UserFragment extends BaseFrameLayout  {
	MainTabActivity activity;
	@ViewInject(id=R.id.header_label_title) TextView txt_header_title;
	@ViewInject(id=R.id.header_btn_left) TextView txt_header_left;
	@ViewInject(id=R.id.header_btn_right) TextView txt_header_right;
	@ViewInject(id=R.id.user_tx ) ImageView img_tx;
	@ViewInject(id=R.id.user_jg) TextView txt_jg;
	@ViewInject(id=R.id.user_sfz) TextView txt_sfz;
	@ViewInject(id=R.id.user_btn_exit, click="onExitClick") Button btn_exit;
	@ViewInject(id=R.id.user_menu, itemClick="onItemClick") ListView menuList;
	
	GncdListAdapter adapter;
	
	List<ImageItem> items;
	
	public UserFragment(Context context){
		super(context, R.layout.fragment_user);
		this.activity = (MainTabActivity) context;
	}

	@Override
	public void initializeView() {
		TAppYh user = BankApplication.mInstance.getCurrentUser();
		txt_header_left.setVisibility(View.GONE);
		txt_header_right.setVisibility(View.GONE);
		txt_header_title.setText(user.getXm());
		if(user.getZh().length() > 5) {
			txt_sfz.setText("身份证：" + DesensitizeUtil.around(user.getZh(), 2, 2) );
		}else if(user.getZh().length() > 2){
			txt_sfz.setText("身份证：" + DesensitizeUtil.left(user.getZh(), 2) );
		}else if(user.getZh().length() > 1){
			txt_sfz.setText("身份证："  + DesensitizeUtil.left(user.getZh(), 1) );
		}else {
			txt_sfz.setText("身份证："  + user.getZh());
		}

		if(user.getXb() == 1){		// 男
			Glide.with(getContext()).load(R.drawable.bank_tx_man).into(img_tx);
		}else{
			Glide.with(getContext()).load(R.drawable.bank_tx_woman).into(img_tx);
		}
		
		txt_jg.setText(user.getBmmc());
		items = MenuUtil.getMenuByName("我的");
		ImageItem lastItem = new ImageItem();
		lastItem.setImg("setting/setting_settingdt.png");
		lastItem.setKey("");
		lastItem.setType(0);
		lastItem.setGnlj("com.gdth.bank.activity.offlineDownActivity");
		lastItem.setValue("离线地图");
		items.add(lastItem);

//		ImageItem lastItem2 = new ImageItem();
//		lastItem2.setImg("setting/setting_settingpw.png");
//		lastItem2.setKey("");
//		lastItem2.setType(0);
//		lastItem2.setGnlj("com.gdth.bank.activity.UnbindYHActivity");
//		lastItem2.setValue("app解绑");
//		items.add(lastItem2);

		ImageItem lastItem1 = new ImageItem();
		lastItem1.setImg("setting/setting_settingpw.png");
		lastItem1.setKey("");
		lastItem1.setType(0);
		lastItem1.setGnlj("com.gdth.bank.activity.ChangePasswordActivity");
		lastItem1.setValue("修改密码");
		items.add(lastItem1);
		adapter = new GncdListAdapter(this.context, items);
		menuList.setAdapter(adapter);
		img_tx.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				onDetailClick(view);
			}
		});
	}
	
	/**
	 * 退出事件
	 * @param arg0
	 */
	public void onExitClick(View arg0){
		activity.onExitClick();
	}

	/**
	 * 查看用户详细信息
	 * @param arg0
	 */
	public void onDetailClick(View arg0) {
		Intent intent = new Intent(this.context, UserActivity.class);
		this.context.startActivity(intent);
	}

	/**
	 * 菜单点击事件
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 */
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
    	ImageItem mImageItem = items.get(arg2);
    	String title=mImageItem.getValue();
    	joinBykey(mImageItem.getGnlj(),title);
	}
	
	private void joinBykey(String val,String title){
		Intent intent = new Intent();
    	intent.setClassName(this.context, val);
    	intent.putExtra("titlename", title);
    	context.startActivity(intent);
	}

}
